package com.paic.mhis.hcpms.quota.exportword.biz.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.biz.service.KpiCoreDataService;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiConditionDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiGradeDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiSociationDTO;
import com.paic.mhis.hcpms.quota.exportword.biz.service.QueryPerFeeAndTimesService;
import com.paic.mhis.hcpms.quota.exportword.utils.WordReportCalculateHelper;
import com.paic.mhis.hcpms.quota.settlement.common.Constants;
import com.paic.mhis.hcpms.quota.settlement.common.KpiValueConst;

/**
 * Description: 获取住院门诊总费用和者药费占比
 */
@Service("queryPerFeeAndTimesService")
public class QueryPerFeeAndTimesServiceImpl implements QueryPerFeeAndTimesService {

	@Autowired
	private KpiCoreDataService kpiCoreDataService;

	private WordReportCalculateHelper helper = WordReportCalculateHelper.getInstance();

	@Override
	public Map<String, Object> getHosTableData(Map<String, Object> paramsMap) {
		paramsMap.put("visitType", "1");
		paramsMap.put("tableKey", "k1_2_3_1_tableList_end");
		return querySecendPartInfoToRtf(paramsMap);
	}

	@Override
	public Map<String, Object> getOutTableData(Map<String, Object> paramsMap) {
		paramsMap.put("visitType", "2");
		paramsMap.put("tableKey", "k1_3_3_1_tableList_end");
		return querySecendPartInfoToRtf(paramsMap);

	}

	private Map<String, Object> querySecendPartInfoToRtf(Map<String, Object> paramsMap) {
		// 设置查询条件
		String visitType = paramsMap.get("visitType").toString();
		String month = paramsMap.get("currentDate").toString();
		String regionCode = paramsMap.get("regionCode").toString();
		KpiConditionDTO kpiConditionDTO = new KpiConditionDTO();
		kpiConditionDTO.setVisitType(visitType);
		kpiConditionDTO.setKpiTime(month);
		List<String> kpiCodeList = new ArrayList<String>();
		kpiCodeList.add(KpiValueConst.KPI0010);
		kpiCodeList.add(KpiValueConst.KPI0004);
		kpiConditionDTO.setKpiCodeList(kpiCodeList);
		kpiConditionDTO.setPeriod(Constants.perion_13); // 按照月份
		kpiConditionDTO.setSociationCode(regionCode);

		List<String> levelList = new ArrayList<String>();
		levelList.add(Constants.type_4);
		levelList.add(Constants.type_3);
		levelList.add(Constants.type_2);

		kpiConditionDTO.setGradeList(levelList);

		List<KpiGradeDTO> kpiGradeList = this.kpiCoreDataService.queryKpiByGrade(kpiConditionDTO);
		KpiConditionDTO kpi0004Con = helper.getKpiConditionByKpiCode(paramsMap, KpiValueConst.KPI0004);
		KpiConditionDTO kpi0010Con = helper.getKpiConditionByKpiCode(paramsMap, KpiValueConst.KPI0010);
		KpiSociationDTO totalTimesFee = (KpiSociationDTO) this.kpiCoreDataService.querySingleKpiBySoc(kpi0004Con);
		KpiSociationDTO totalPerFee = (KpiSociationDTO) this.kpiCoreDataService.querySingleKpiBySoc(kpi0010Con);
		Map<String, Object> resultMap = createMapByGrade(kpiGradeList, paramsMap, totalTimesFee, totalPerFee);
		return resultMap;
	}

	private Map<String, Object> createMapByGrade(List<KpiGradeDTO> kpiGradeList, Map<String, Object> paramsMap,
			KpiSociationDTO totalTimesFee, KpiSociationDTO totalPerFee) {

		// String visitType = (String) paramsMap.get("visitType");
		String[] grade4 = { "一级及以下医院", "0", "0.00", "0.00", "0.00", "0.00", "0.00" };
		String[] grade3 = { "二级医院", "0", "0.00", "0.00", "0.00", "0.00", "0.00" };
		String[] grade2 = { "三级医院", "0", "0.00", "0.00", "0.00", "0.00", "0.00" };
		String[] totalValue = { "合计", "0", "", "", "0.00", "", "" };
		String totalFeeKpiValue = "0";
		String totalKpiValue = "0.00";
		// if (KpiValueConst.VISIT_HOS.equals(visitType)) {
		// grade2[1] = "0.0";
		// grade3[1] = "0.0";
		// grade4[1] = "0.0";
		// totalValue[1] = "0.0";
		// totalFeeKpiValue = "0.0";
		// }
		for (KpiGradeDTO dto : kpiGradeList) {
			if (Constants.type_4.equals(dto.getGrade())) {
				judGradeToSetVal(grade4, dto, paramsMap);
			} else if (Constants.type_3.equals(dto.getGrade())) {
				judGradeToSetVal(grade3, dto, paramsMap);
			} else if (Constants.type_2.equals(dto.getGrade())) {
				judGradeToSetVal(grade2, dto, paramsMap);
			}
		}
		totalValue[0] = "合计";
		if (totalPerFee != null) {
			// if (KpiValueConst.VISIT_HOS.equals(visitType)) {
			// totalFeeKpiValue =
			// helper.toFixedOnePercent(helper.filterNullValue(totalPerFee.getKpiValue()));
			// } else {
			totalFeeKpiValue = helper.toFixedOne(helper.filterNullValue(totalPerFee.getKpiValue()));
			// }
		}
		totalValue[1] = totalFeeKpiValue;
		if (totalTimesFee != null) {
			totalKpiValue = helper.toFixedPercent(helper.filterNullValue(totalTimesFee.getKpiValue()));
		}
		totalValue[4] = totalKpiValue.toString();
		List<Object> tableList = new ArrayList<Object>();
		tableList.add(grade2);
		tableList.add(grade3);
		tableList.add(grade4);
		tableList.add(totalValue);
		Map<String, Object> resultMap = new HashMap<String, Object>();

		resultMap.put((String) paramsMap.get("tableKey"), tableList);

		return resultMap;
	}

	private void judGradeToSetVal(String[] grade, KpiGradeDTO dto, Map<String, Object> paramsMap) {
		if (KpiValueConst.KPI0010.equals(dto.getKpiCode())) {
			setGradeVal(grade, dto, paramsMap);
		} else {
			setMoreGradeVal(grade, dto, paramsMap);
		}
	}

	private void setGradeVal(String[] grade, KpiGradeDTO dto, Map<String, Object> paramsMap) {
		// String visitType = (String) paramsMap.get("visitType");
		// if (KpiValueConst.VISIT_HOS.equals(visitType)) {
		// grade[1] =
		// (helper.toFixedOnePercent(helper.filterNullValue(dto.getKpiValue()))).toString();
		// } else {
		grade[1] = (helper.toFixedOne(helper.filterNullValue(dto.getKpiValue()))).toString();
		// }
		grade[2] = (helper.toFixedTwoPercent(helper.filterNullValue(dto.getPeriodRatio())).toString());
		grade[3] = (helper.toFixedTwoPercent(helper.filterNullValue(dto.getChainRatio())).toString());
	}

	private void setMoreGradeVal(String[] grade, KpiGradeDTO dto, Map<String, Object> paramsMap) {
		grade[4] = (helper.toFixedTwoPercent(helper.filterNullValue(dto.getKpiValue()))).toString();
		grade[5] = (helper.toFixedTwoPercent(helper.filterNullValue(dto.getPeriodRatio())).toString());
		grade[6] = (helper.toFixedTwoPercent(helper.filterNullValue(dto.getChainRatio())).toString());
	}
}
